﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MdcService.Configuration;
using MdcService.Log;
using MdcService.Structure;
using MdcService.Structure.Enum;
using Nancy.Hosting.Self;

namespace MdcService.Service
{
    public class RestService:IService
    {
        private NancyHost _nancyHost;
        private ConfigurationService _configurations;
        private RunningContext _runningContext;
        private ILogging _logging;

        public event EventHandler<EventArgs> Started;
        public event EventHandler<EventArgs> Stopped;


        public RestService(ConfigurationService configurations, RunningContext runningContext, ILogging logging)
        {
            _configurations = configurations;
            _runningContext = runningContext;
            _logging = logging;
           
        }


        public void Start()
        {
            if (_runningContext.ResponsingServiceState == ServiceState.Running) return;

            if (_configurations.Configuration.RestService)
            {
                _nancyHost = new NancyHost(new Uri("http://192.168.1.233:80"));
                _nancyHost.Start();
            }
            _runningContext.ResponsingServiceState = ServiceState.Running;
            Started?.Invoke(this, new EventArgs());

        }

        public void Stop()
        {
            if (_runningContext.ResponsingServiceState == ServiceState.Stopped) return;

            if (_configurations.Configuration.RestService)
            {
                _nancyHost.Stop();
                _nancyHost.Dispose();
            }
            _runningContext.ResponsingServiceState = ServiceState.Stopped;
            Stopped?.Invoke(this, new EventArgs());

        }


    }
}
